package botcc.androidBot;

import java.io.FileNotFoundException;
import java.net.URL;

import org.aitools.programd.Core;
import org.aitools.programd.CoreShutdownHook;
import org.aitools.programd.interfaces.Console;
import org.aitools.programd.util.URLTools;

public class AndroidProgramDAdapter {
	 /** The Core to which this console will be attached. */
    private Core core;

    /** The console. */ 
    private Console console;
  
    private AndroidProgramDAdapter(String corePropertiesPath) throws FileNotFoundException
    {
        URL baseURL = URLTools.createValidURL(System.getProperty("user.dir"));
        this.console = new Console();
        this.core = new Core(baseURL, URLTools.createValidURL(corePropertiesPath));
        this.console.attachTo(this.core);
    }

    /**
     * Starts the shell (if enabled) and sends the connect string.
     */
    public void run()
    {
        // Send the connect string.
        this.core.processResponse(this.core.getSettings().getConnectString());
        this.console.startShell();
    }

    /**
     * Starts up the SimpleConsole configuration. Required arguments are:
     * <ul>
     * <li><code>-c, --core-properties     the path to the core configuration (XML properties) file</code></li>
     * </ul>
     * 
     * @param argv
     */
    public static void main(String[] argv)
    {
        String corePropertiesPath = argv[0];

        if (corePropertiesPath == null)
        {
            System.err.println("You must specify a core properties path.");
            System.exit(1);
        }

        AndroidProgramDAdapter console = null;
        try
        {
            console = new AndroidProgramDAdapter(corePropertiesPath);
        }
        catch (FileNotFoundException e)
        {
            System.err.println("Core properties file \"" + corePropertiesPath + "\" not found.");
        }
        // Add a shutdown hook so the Core will be properly shut down if the
        // system exits.
        Runtime.getRuntime().addShutdownHook(new CoreShutdownHook(console.core));
        console.run();
    }
}
